//
//  BarChartViewController.m
//  cocoapodText
//
//  Created by apple on 16/8/5.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "BarChartViewController.h"
@interface BarChartViewController ()
@property (nonatomic, strong) IBOutlet NSArray *options;
@end

@implementation BarChartViewController

BarChartView *sampleBarChartView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initBarChartView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)initBarChartView{
    sampleBarChartView=[[BarChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    sampleBarChartView.backgroundColor = [UIColor whiteColor];
    sampleBarChartView.delegate = self;
    [self.view addSubview:sampleBarChartView];
    [self initBarChartStyleAndData];
}


-(void)initBarChartStyleAndData{
    [self setupBarLineChartView:sampleBarChartView];
    [self setBarChartParam:sampleBarChartView];
    [self setLegendStyle:sampleBarChartView];
    [self initChartData];
}

- (void)setupBarLineChartView:(BarLineChartViewBase *)chartView
{
    //描述
    chartView.descriptionText = @"descriptionText";
    //无数据默认显示
    chartView.noDataTextDescription = @"You need to provide data for the chart.";
    //网格背景
    chartView.drawGridBackgroundEnabled = NO;
    chartView.dragEnabled = YES;
    [chartView setScaleEnabled:YES];
    chartView.pinchZoomEnabled = NO;
    // ChartYAxis *leftAxis = chartView.leftAxis;
    //x轴
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    chartView.rightAxis.enabled = NO;
}

-(void)setBarChartParam:(BarChartView *)chartView{
    //阴影填充效果
    chartView.drawBarShadowEnabled = NO;
    //柱状图高度数字是否放置于柱状图上方（NO为内部）
    chartView.drawValueAboveBarEnabled = YES;
    chartView.maxVisibleValueCount = 60;
    //X轴左边样式设置
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    xAxis.labelFont = [UIFont systemFontOfSize:10.f];
    xAxis.drawGridLinesEnabled = NO;
    xAxis.spaceBetweenLabels = 2.0;
    //Y轴左边样式设置
    ChartYAxis *leftAxis = chartView.leftAxis;
    leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
    leftAxis.labelCount = 8;
    leftAxis.valueFormatter = [[NSNumberFormatter alloc] init];
    leftAxis.valueFormatter.maximumFractionDigits = 1;
    leftAxis.valueFormatter.negativeSuffix = @" $";
    leftAxis.valueFormatter.positiveSuffix = @" $";
    leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
    leftAxis.spaceTop = 0.15;
    leftAxis.axisMinValue = 0.0; // this replaces startAtZero = YES
    //Y轴右边样式设置
    ChartYAxis *rightAxis = chartView.rightAxis;
    rightAxis.enabled = YES;
    rightAxis.drawGridLinesEnabled = NO;
    rightAxis.labelFont = [UIFont systemFontOfSize:10.f];
    rightAxis.labelCount = 8;
    rightAxis.valueFormatter = leftAxis.valueFormatter;
    rightAxis.spaceTop = 0.15;
    rightAxis.axisMinValue = 0.0; // this replaces startAtZero = YES
}

//设置图例样式
-(void)setLegendStyle:(BarChartView *)chartView{
    //图例在柱状图中的位置
    chartView.legend.position = ChartLegendPositionBelowChartLeft;
    //图示样式: 方形、线条、圆形
    chartView.legend.form = ChartLegendFormSquare;
    //图示大小
    chartView.legend.formSize = 9.0;
    //字体大小
    chartView.legend.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
    //间隔
    chartView.legend.xEntrySpace = 4.0;
}

- (void)initChartData
{
    [self setDataCount:10 range:100 chart:sampleBarChartView];
}

- (void)setDataCount:(int)count range:(double)range chart:(BarChartView *)chartView
{
    //x轴文字
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++){
        [xVals addObject:[@"X" stringByAppendingString:[NSString stringWithFormat:@"%d",i]]];
    }
    //y轴高度
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++){
        double mult = (range + 1);
        double val =  (double) (arc4random_uniform(mult));
        [yVals addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:i]];
    }
    
    BarChartDataSet *set1 = nil;
    if (chartView.data.dataSetCount > 0) {
        set1 = (BarChartDataSet *)chartView.data.dataSets[0];
        set1.yVals = yVals;
        chartView.data.xValsObjc = xVals;
        [chartView.data notifyDataChanged];
        [chartView notifyDataSetChanged];
    }else{
        set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:@"DataSet"];
        set1.barSpace = 0.35;
        [set1 setColors:ChartColorTemplates.material];
        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:set1];
        BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
        [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
        chartView.data = data;
    }
}


#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}

@end